//
//  TravelTourThreeTableViewCell.m
//  ToWhere
//
//  Created by dllo on 15/11/18.
//  Copyright © 2015年 www.TE.com. All rights reserved.
//

#import "TravelTourThreeTableViewCell.h"
#import <SDWebImageManager.h>
#import <UIImageView+WebCache.h>
#import "TravelTopicDetailsOneTableViewCell.h"
#import "TravelTourCollectionViewCell.h"
#import "TravelTourSmallModel.h"
#import "TravelTourTwoCollectionViewCell.h"
#import "TravelTourImagePicViewController.h"

@interface TravelTourThreeTableViewCell ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (nonatomic, retain) UICollectionView *collectionView;

@property (nonatomic, retain) UILabel *labelUserName;

@property (nonatomic, retain) UILabel *labelStart;

@property (nonatomic, retain) UILabel *LabelD;

@end



@implementation TravelTourThreeTableViewCell

-(void)dealloc
{
    [_collectionView release];
    [_labelUserName release];
    [_labelStart release];
    [_LabelD release];
    [_model release];
    [super dealloc];
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        
        [self createView];
    }
    
    return self;
}

-(void)createView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    [flowLayout setMinimumInteritemSpacing:5];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    [self.collectionView setScrollsToTop:NO];
    [self.collectionView setDelegate:self];
    [self.collectionView setDataSource:self];
    
    [self.collectionView registerClass:[TravelTourCollectionViewCell class] forCellWithReuseIdentifier:@"TravelTourCollectionViewCell"];
    
    [self.collectionView registerClass:[TravelTourTwoCollectionViewCell class] forCellWithReuseIdentifier:@"TravelTourTwoCollectionViewCell"];
    
    [self.collectionView setShowsHorizontalScrollIndicator:NO];
    [self.collectionView setBackgroundColor:[UIColor clearColor]];
    [self addSubview:self.collectionView];
    
    
    
    self.LabelD = [[UILabel alloc]init];
    self.LabelD.font =[ UIFont systemFontOfSize:14 *kWidth];
    self.LabelD.textColor = [UIColor lightGrayColor];
    self.LabelD.numberOfLines = 0;
    [self addSubview:self.LabelD];
    [_LabelD release];
    
    
    self.labelUserName = [[UILabel alloc]init];
    self.labelUserName.font = [UIFont systemFontOfSize:12 *kWidth];
    
    self.labelUserName.textColor = [UIColor lightGrayColor];
    
    [self addSubview:self.labelUserName];
    [_labelUserName release];
    
    self.labelStart = [[UILabel alloc]init];
    self.labelStart.font = [UIFont systemFontOfSize:12 *kWidth];
    
    self.labelStart.textColor = [UIColor lightGrayColor];
    self.labelStart.textAlignment = NSTextAlignmentRight;
    [self addSubview:self.labelStart];
    [_labelStart release];
    
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    self.LabelD.frame = CGRectMake(5 *kWidth, 10*kHeight, self.frame.size.width - 10 *kWidth, [TravelTopicDetailsOneTableViewCell heightOfLabel:self.model.DES andTextFont:[UIFont systemFontOfSize:14*kWidth] andTextWidth:self.frame.size.width  - 10 *kWidth]);
    
    if ([[self.model arrNotes] count] ==0) {
        
        self.collectionView.frame = CGRectMake(0, 0, 0, 0);
        self.labelUserName.frame = CGRectMake(5 *kWidth, 10 *kHeight +self.LabelD.frame.size.height, self.frame.size.width / 2 - 5 *kWidth, 20 *kHeight);
        self.labelStart.frame = CGRectMake(self.frame.size.width / 2, self.labelUserName.frame.origin.y, self.frame.size.width /2, 20 *kHeight);
      
    }else{
        
        self.collectionView.frame = CGRectMake(10 *kWidth, self.LabelD.frame.size.height + 15*kHeight, self.frame.size.width - 20 *kWidth, 150 *kHeight);
        self.labelUserName.frame = CGRectMake(5 *kWidth, self.collectionView.frame.origin.y + self.collectionView.frame.size.height + 5*kHeight, self.frame.size.width / 2 - 5 *kWidth, 20 *kHeight);
        self.labelStart.frame = CGRectMake(self.frame.size.width / 2, self.labelUserName.frame.origin.y, self.frame.size.width /2 - 10*kWidth, 20 *kHeight);
    }
    
}

-(void)setModel:(TravelTourMiddleModel *)model
{
    if (_model != model) {
        [_model release];
        _model = [model retain];
    }
  
    [self.collectionView reloadData];
    
    if (![model.DES isKindOfClass:[NSNull class]])
    {  
    NSArray *arr = [model.DES componentsSeparatedByString:@"#"];
    
    NSString *str = [NSString stringWithFormat:@"#%@#",arr[1]];
  
    NSMutableAttributedString *attString = [[NSMutableAttributedString alloc]
initWithString:model.DES];
        [attString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, str.length)];
    
    [self.LabelD setAttributedText:attString];
  
            }
     
    if (![[[model.dicTrip valueForKey:@"user"] valueForKey:@"name"] isKindOfClass:[NSNull class]]) {
        
        self.labelUserName.text = [[model.dicTrip valueForKey:@"user"] valueForKey:@"name"];
    }
    
    if (![[model.dicTrip valueForKey:@"start_date"] isKindOfClass:[NSNull class]]) {
          self.labelStart.text = [model.dicTrip valueForKey:@"start_date"];
    }
    
}

#pragma mark 返回分区数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
#pragma mark 返回单元格
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
        if (self.model.arrNotes ==nil) {
        
        return nil;
    }else{
        
        if ([[self.model.arrNotes[indexPath.row] photo_url] length] != 0) {
            
        
        
                TravelTourCollectionViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"TravelTourCollectionViewCell" forIndexPath:indexPath];
            
            cell.model = self.model.arrNotes[indexPath.row];
            return cell;
        }else{
            
                        TravelTourTwoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TravelTourTwoCollectionViewCell" forIndexPath:indexPath];
                        cell.model = self.model.arrNotes[indexPath.row];
                        return cell;
                        
  
        }
       
           }
   
}



#pragma mark返回单元格数量
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    if (self.model.arrNotes.count == 0) {
        return 0;
    }else{
 
        
    return self.model.arrNotes.count;
    
    }
    
}
#pragma mark 单元格尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
  
    if (self.model.arrNotes.count == 0)
    {
        return CGSizeMake(0, 0 );
    }else{
        
        TravelTourSmallModel *model = self.model.arrNotes[indexPath.row];
        CGFloat ht = [model.height floatValue];
        CGFloat wd = [model.width floatValue];
        
        CGFloat x = (int) wd/ht *150*kHeight;
        
            return CGSizeMake( x, 150*kHeight );
        
    }

}

#pragma mark 点击事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
       
    [self.delegate pushToImageOivVC:self.model.arrNotes Number:indexPath.row];
    
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
